/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Text,  Button, Input, useTheme, InputLine,
} from "customize-easy-ui-component";
import {InspectRecordLayout, InternalItemProps, SelectHookfork, useItemInputControl} from "../common/base";
import {Each_ZdSetting, tabIBlistCb, useTableEditor} from "../hook/useRepTableEditor";
import {EditStorageContext} from "../StorageContext";
import {EvaluateItemConfig, itemResultUnqualifiedEvaluate} from "./config";

//没有给每一个项目都安排 '复检日期'字段的。
export const config整改项表=[['编号','no',84],['不合格内容','b',270],['复检结果','rs',50,(obj, setObj)=>{
  return <SelectHookfork value={ obj?.rs ||''} onChange={e => setObj({ ...obj, rs: e.currentTarget.value||undefined}) }/>
}]] as Each_ZdSetting[];

interface Props  extends InternalItemProps{
    label: string;
    noCB?: (no:string,et:EvaluateItemConfig)=>string;
    /**不符合项目 依据的iClass拆解成了 3张表；
    * */
    iClass?: string;
    //判定不合格项目，首先也得要构建整个检验项目全表的。
    setup: ({verId, repId, theme} :{verId:string, repId:string,theme:any}) => { [key: string]: any[] };
}

/**评估报告采用的：不合格 分成3张表，复检日期才有一个。
 * 原始记录： (二)存在的风险不符合项目三个表，： 不合格按照类别= A,B,undefined, 区分为三个表 ，只显示3个表其中一个：
 * @param setup  每个模板的setupItemAreaRoute检验项目配置构建函数
 * 通常不合格在数据库纯纯{no:stno, c:et.iclas, b: }，但是按照iClass拆分的就没有独立存储c类别字段的。
 * */
export const ItemRecheckEvaluateSplit=
    React.forwardRef((
        { children, show ,alone=true,repId,verId,label,noCB,setup,iClass}:Props,  ref
    ) => {
      const theme= useTheme();
        const nameFJd='复检日'+(iClass??'');       //复检日期每个项目类别才有一个 存储;

      const impressionismAs =React.useMemo(() => {
        return setup({verId:verId!, repId:repId!, theme});
      }, [verId, repId, theme]);
      const tableName='unq'+(iClass??'');
      const getInpFilter = React.useCallback((par: any) => {
            let fields={} as any;
            fields[tableName] =par[tableName];
            fields[nameFJd] =par[nameFJd];
            return fields;
      }, []);
      const {inp, setInp} = useItemInputControl({ ref });
      const {storage, setStorage} =React.useContext(EditStorageContext) as any;
      const 默认复检表 =React.useMemo(()=>itemResultUnqualifiedEvaluate(storage, impressionismAs?.Item,
                                {noCB: noCB, clsSplit:true, iClass } ),
          [storage,impressionismAs?.Item]);
      const headview=<Text variant="h5">
        {label}；项目类别都是：{iClass??'<空>'}。
      </Text>;
      //不是真的默认不变的不能用  defaultV:默认复检表,
      const [renderTab]=useTableEditor({inp, setInp,  headview,
        config: config整改项表, table: tableName, defaultV:默认复检表, noDelAdd:true, fixColumn:1,maxRf:2,saveFixC:true});

      return (
          <InspectRecordLayout inp={inp} setInp={setInp}  getInpFilter={getInpFilter} show={show}
                               alone={alone}  label={label} column={0}>
            <Button intent='primary' onPress={() =>{
                  let arrUnq=itemResultUnqualifiedEvaluate(storage, impressionismAs?.Item,
                                              {noCB: noCB, clsSplit:true, iClass, classes:[undefined,'A','B'] } );
                          setStorage({...storage, [tableName]: arrUnq});
                    }} >
              依据记录来初始化本表默认值
            </Button>但不能重复初始化！
            <hr/>
            {renderTab}
            <InputLine  label='复检日期:' >
              <Input  value={inp?.[nameFJd] ||''}  type='date'
                      onChange={e => setInp({ ...inp, [nameFJd]: e.currentTarget.value}) } />
            </InputLine>
          </InspectRecordLayout>
      );
    } );
interface ItemRecheckEvaluateSplitRProps  extends InternalItemProps{
    label: string;
    noCB?: (no:string,et:EvaluateItemConfig)=>string;
    /**不符合项目 依据的iClass拆解成了 3张表；
     * */
    iClass?: string;
    //判定不合格项目，首先也得要构建整个检验项目全表的。
    setup: ({rep,orc,theme} :{rep:any,orc?:any,theme:any}) => { [key: string]: any[] };
}
export const itemA复检=['复检日','复检日A','复检日B'];
//用rep替代repId,verId,的注入； 替代ItemRecheckEvaluateSplit
export const ItemRecheckEvaluateSplitR=
    React.forwardRef((
        { children, show ,alone=true,label,noCB,setup,iClass,rep}:ItemRecheckEvaluateSplitRProps,  ref
    ) => {
        const theme= useTheme();
        const nameFJd='复检日'+(iClass??'');       //复检日期每个项目类别才有一个 存储;

        const impressionismAs =React.useMemo(() => {
            return setup({rep, theme});
        }, [rep, theme]);
        const tableName='unq'+(iClass??'');
        const getInpFilter = React.useCallback((par: any) => {
            let fields={} as any;
            fields[tableName] =par[tableName];
            fields[nameFJd] =par[nameFJd];
            return fields;
        }, []);
        const {inp, setInp} = useItemInputControl({ ref });
        const {storage, setStorage} =React.useContext(EditStorageContext) as any;
        const 默认复检表 =React.useMemo(()=>itemResultUnqualifiedEvaluate(storage, impressionismAs?.Item,
                {noCB: noCB, clsSplit:true, iClass } ),
            [storage,impressionismAs?.Item]);
        const headview=<Text variant="h5">
            {label}；项目类别都是：{iClass??'<空>'}。
        </Text>;
        //不是真的默认不变的不能用  defaultV:默认复检表,
        const [renderTab]=useTableEditor({inp, setInp,  headview,
            config: config整改项表, table: tableName, defaultV:默认复检表, noDelAdd:true, fixColumn:1,maxRf:2,saveFixC:true});

        return (
            <InspectRecordLayout inp={inp} setInp={setInp}  getInpFilter={getInpFilter} show={show}
                                 alone={alone}  label={label} column={0}>
                <Button intent='primary' onPress={() =>{
                    let arrUnq=itemResultUnqualifiedEvaluate(storage, impressionismAs?.Item,
                        {noCB: noCB, clsSplit:true, iClass, classes:[undefined,'A','B'] } );
                    setStorage({...storage, [tableName]: arrUnq});
                }} >
                    依据记录来初始化本表默认值
                </Button>但不能重复初始化！
                <hr/>
                {renderTab}
                <InputLine  label='复检日期:' >
                    <Input  value={inp?.[nameFJd] ||''}  type='date'
                            onChange={e => setInp({ ...inp, [nameFJd]: e.currentTarget.value}) } />
                </InputLine>
            </InspectRecordLayout>
        );
    } );

const 措施选=['维护保养','更换','修理','增设']
export const config整改措施表=[['编号','no',84],['不合格内容','b',270],['整改措施','c',75,tabIBlistCb(措施选)],['复检结果','rs',50,(obj, setObj)=>{
    return <SelectHookfork value={ obj?.rs ||''} onChange={e => setObj({ ...obj, rs: e.currentTarget.value||undefined}) }/>
}]] as Each_ZdSetting[];
/*有整改措施的版本：
【约定】考虑自定义的类别需求： _Oitems #要给impressionismAs增加一个依赖注入！
* */
export const ItemRecheckEvaluateSplitZG=
    React.forwardRef((
        { children, show ,alone=true,label,noCB,setup,iClass,rep}:ItemRecheckEvaluateSplitRProps,  ref
    ) => {
        const theme= useTheme();
        const nameFJd='复检日'+(iClass??'');       //复检日期每个项目类别才有一个 存储;
        const tableName='unq'+(iClass??'');
        const getInpFilter = React.useCallback((par: any) => {
            let fields={} as any;
            fields[tableName] =par[tableName];
            fields[nameFJd] =par[nameFJd];
            fields._Oitems =par?._Oitems;
            return fields;
        }, []);
        const {inp, setInp} = useItemInputControl({ ref });
        const impressionismAs =React.useMemo(() => {
            return setup({rep, orc:inp, theme});
        }, [rep,inp?._Oitems, theme]);
        const {storage, setStorage} =React.useContext(EditStorageContext) as any;
        const 默认复检表 =React.useMemo(()=>itemResultUnqualifiedEvaluate(storage, impressionismAs?.Item,
                {noCB: noCB, clsSplit:true, iClass } ),
            [storage,impressionismAs?.Item]);
        const headview=<Text variant="h5">
            {label}；项目类别都是：{iClass??'<空>'}。
        </Text>;
        //不是真的默认不变的不能用  defaultV:默认复检表,
        const [renderTab]=useTableEditor({inp, setInp,  headview,
            config: config整改措施表, table: tableName, defaultV:默认复检表, noDelAdd:true, fixColumn:1,maxRf:2,saveFixC:true});

        return (
            <InspectRecordLayout inp={inp} setInp={setInp}  getInpFilter={getInpFilter} show={show}
                                 alone={alone}  label={label} column={0}>
                <Button intent='primary' onPress={() =>{
                    let arrUnq=itemResultUnqualifiedEvaluate(storage, impressionismAs?.Item,
                        {noCB: noCB, clsSplit:true, iClass, classes:[undefined,'A','B'] } );
                    setStorage({...storage, [tableName]: arrUnq});
                }} >
                    依据记录来初始化本表默认值
                </Button>但不能重复初始化！
                <hr/>
                {renderTab}
                <InputLine  label='复检日期:' >
                    <Input  value={inp?.[nameFJd] ||''}  type='date'
                            onChange={e => setInp({ ...inp, [nameFJd]: e.currentTarget.value}) } />
                </InputLine>
            </InspectRecordLayout>
        );
} );
